package com.hmeter.aws.s3.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;

import org.apache.jmeter.gui.util.VerticalPanel;
import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.gui.JLabeledChoice;
import org.apache.jorphan.gui.JLabeledTextField;

import com.hmeter.aws.s3.S3Sampler;

public class S3SamplerGui extends AbstractSamplerGui {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6983549954165308638L;

	private JLabeledTextField awsCredentialsFilePathTextField = null;
	private JLabeledTextField bucketNameTextField = null;
	private JLabeledTextField keyTextField = null;
	private JLabeledTextField inputFilePathTextField = null;
	private JLabeledTextField outputFilePathTextField = null;
	private JLabeledChoice regionLabeledChoice = null;
	private JCheckBox createNewBucketCheckBox = null;
	private JCheckBox uploadFileToBucketCheckBox = null;
	private JCheckBox readFileFromBucketCheckBox = null;
	private JCheckBox downloadFileFromBucketCheckBox = null;
	private JCheckBox getListOfObjectsInBucketCheckBox = null;
	private JCheckBox deleteObjectFromBucketCheckBox = null;
	private JCheckBox deleteBucketCheckBox = null;
	
	public S3SamplerGui() {
		super();
		init();
	}
	
	public void init() {
		setLayout(new BorderLayout());
        setBorder(makeBorder());
        add(makeTitlePanel(), BorderLayout.NORTH);
            
        awsCredentialsFilePathTextField = new JLabeledTextField("AWS credentials property file path");
        bucketNameTextField = new JLabeledTextField("Bucket name");
        keyTextField = new JLabeledTextField("Key");
        inputFilePathTextField = new JLabeledTextField("Input file path");
        outputFilePathTextField = new JLabeledTextField("Output file path");
        regionLabeledChoice = new JLabeledChoice("Region", new String[] { "", "US_Standard", "US_West", "US_West_2", "EU_Ireland", "AP_Singapore", "AP_Tokyo", "SA_SaoPaulo" });
        createNewBucketCheckBox = new JCheckBox("Create new bucket");
        uploadFileToBucketCheckBox = new JCheckBox("Upload file to bucket");
        readFileFromBucketCheckBox = new JCheckBox("Read file from bucket");
        downloadFileFromBucketCheckBox = new JCheckBox("Download file from bucket");
        getListOfObjectsInBucketCheckBox = new JCheckBox("Get list of objects in bucket");
        deleteObjectFromBucketCheckBox = new JCheckBox("Delete object from bucket");
        deleteBucketCheckBox = new JCheckBox("Delete bucket");
        
        ButtonGroup options = new ButtonGroup();
        options.add(createNewBucketCheckBox);
        options.add(uploadFileToBucketCheckBox);
        options.add(readFileFromBucketCheckBox);
        options.add(downloadFileFromBucketCheckBox);
        options.add(getListOfObjectsInBucketCheckBox);
        options.add(deleteObjectFromBucketCheckBox);
        options.add(deleteBucketCheckBox);
        
        //main panel
        VerticalPanel panel = new VerticalPanel();
        panel.setBorder(BorderFactory.createEtchedBorder());
        
        //input data panel
        VerticalPanel inputPanel = new VerticalPanel();
        inputPanel.setBorder(BorderFactory.createTitledBorder("Input data"));
        
        inputPanel.add(awsCredentialsFilePathTextField);
        inputPanel.add(bucketNameTextField);
        inputPanel.add(keyTextField);
        inputPanel.add(regionLabeledChoice, BorderLayout.WEST);
        inputPanel.add(inputFilePathTextField);
        inputPanel.add(outputFilePathTextField);
        
        VerticalPanel s3OperationsPanel = new VerticalPanel();
        s3OperationsPanel.setBorder(BorderFactory.createTitledBorder("S3 operations"));
        s3OperationsPanel.add(createNewBucketCheckBox);
        s3OperationsPanel.add(uploadFileToBucketCheckBox);
        s3OperationsPanel.add(readFileFromBucketCheckBox);
        s3OperationsPanel.add(downloadFileFromBucketCheckBox);
        s3OperationsPanel.add(getListOfObjectsInBucketCheckBox);
        s3OperationsPanel.add(deleteObjectFromBucketCheckBox);
        s3OperationsPanel.add(deleteBucketCheckBox);
                
        panel.add(inputPanel);
        panel.add(s3OperationsPanel);
        
        add(panel, BorderLayout.CENTER);
        
        changePanelComponentsAccess(createNewBucketCheckBox, keyTextField,inputFilePathTextField,outputFilePathTextField);
        changePanelComponentsAccess(uploadFileToBucketCheckBox, regionLabeledChoice,outputFilePathTextField);
        changePanelComponentsAccess(readFileFromBucketCheckBox, regionLabeledChoice,inputFilePathTextField,outputFilePathTextField);
        changePanelComponentsAccess(downloadFileFromBucketCheckBox, regionLabeledChoice,inputFilePathTextField);
        changePanelComponentsAccess(getListOfObjectsInBucketCheckBox, keyTextField,regionLabeledChoice,inputFilePathTextField,outputFilePathTextField);
        changePanelComponentsAccess(deleteObjectFromBucketCheckBox, regionLabeledChoice,inputFilePathTextField,outputFilePathTextField);
        changePanelComponentsAccess(deleteBucketCheckBox, keyTextField,regionLabeledChoice,inputFilePathTextField,outputFilePathTextField);
	}
	
	public void changePanelComponentsAccess(JCheckBox checkBox, final Component...components)
	{		
		checkBox.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED)
				{
					for (Component c : components)
					{
						c.setEnabled(false);
					}
				}
				else if (e.getStateChange() == ItemEvent.DESELECTED)
				{
					for (Component c : components)
					{
						c.setEnabled(true);
					}
				}
			}
		}
		);
	}
	
	@Override
	public void clearGui()
	{
		super.clearGui();
		awsCredentialsFilePathTextField.setText("");
		bucketNameTextField.setText("");
		keyTextField.setText("");
		regionLabeledChoice.setText("");
		inputFilePathTextField.setText("");
		outputFilePathTextField.setText("");
		createNewBucketCheckBox.setSelected(true);
		uploadFileToBucketCheckBox.setSelected(false);
		readFileFromBucketCheckBox.setSelected(false);
		downloadFileFromBucketCheckBox.setSelected(false);
		getListOfObjectsInBucketCheckBox.setSelected(false);
		deleteObjectFromBucketCheckBox.setSelected(false);
		deleteBucketCheckBox.setSelected(false);
	}
	
	@Override
	public String getLabelResource() {
		return getClass().getSimpleName();
	}
	
	@Override
	public String getStaticLabel() {
	    return "AWS S3 Sampler";
	}
	
	@Override
	public TestElement createTestElement() {
		S3Sampler s3Sampler = new S3Sampler();
		modifyTestElement((TestElement) s3Sampler);
		return (TestElement) s3Sampler;
	}
	
	@Override
	public void modifyTestElement(TestElement element) {
		super.configureTestElement(element);
		if (element instanceof S3Sampler)
		{
			S3Sampler s3Sampler = (S3Sampler) element;
			s3Sampler.setAwsCredentialsFilePath(awsCredentialsFilePathTextField.getText());
			s3Sampler.setBucketName(bucketNameTextField.getText());
			s3Sampler.setKey(keyTextField.getText());
			s3Sampler.setRegion(regionLabeledChoice.getText());
			s3Sampler.setInputFilePath(inputFilePathTextField.getText());
			s3Sampler.setOutputFilePath(outputFilePathTextField.getText());
			s3Sampler.setCreateNewBucketBool(createNewBucketCheckBox.isSelected());
			s3Sampler.setUploadFileToBucketBool(uploadFileToBucketCheckBox.isSelected());
			s3Sampler.setReadFileFromBucketBool(readFileFromBucketCheckBox.isSelected());
			s3Sampler.setDownloadFileFromBucketBool(downloadFileFromBucketCheckBox.isSelected());
			s3Sampler.setGetListOfObjectsInBucketBool(getListOfObjectsInBucketCheckBox.isSelected());
			s3Sampler.setDeleteObjectFromBucketBool(deleteObjectFromBucketCheckBox.isSelected());
			s3Sampler.setDeleteBucketBool(deleteBucketCheckBox.isSelected());
		}
	}
	
	@Override
	public void configure(TestElement element)
	{
		super.configure(element);
		if (element instanceof S3Sampler)
		{
			S3Sampler s3Sampler = (S3Sampler) element;
			awsCredentialsFilePathTextField.setText(s3Sampler.getAwsCredentialsFilePath());
			bucketNameTextField.setText(s3Sampler.getBucketName());
			keyTextField.setText(s3Sampler.getKey());
			regionLabeledChoice.setText(s3Sampler.getRegion());
			inputFilePathTextField.setText(s3Sampler.getInputFilePath());
			outputFilePathTextField.setText(s3Sampler.getOutputFilePath());
			createNewBucketCheckBox.setSelected(s3Sampler.isCreateNewBucketBool());
			uploadFileToBucketCheckBox.setSelected(s3Sampler.isUploadFileToBucketBool());
			readFileFromBucketCheckBox.setSelected(s3Sampler.isReadFileFromBucketBool());
			downloadFileFromBucketCheckBox.setSelected(s3Sampler.isDownloadFileFromBucketBool());
			getListOfObjectsInBucketCheckBox.setSelected(s3Sampler.isGetListOfObjectsInBucketBool());
			deleteObjectFromBucketCheckBox.setSelected(s3Sampler.isDeleteObjectFromBucketBool());
			deleteBucketCheckBox.setSelected(s3Sampler.isDeleteBucketBool());
		}
	}

}
